﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Pasta.Medio.Concrete;
using Pasta.Medio.Configuration;
using Pasta.Medio.Domain;
using Pasta.Medio.Properties;

namespace Pasta.Medio.Controls
{
	public partial class CatalogTab : TabBase
	{
		private AppSettings Settings = Program.Settings;
		private SortableBindingList<Recording> Catalog = Program.Catalog;

		public CatalogTab()
		{
			InitializeComponent();
			Settings = Program.Settings;
		}

		public override void RefreshPage()
		{
			base.RefreshPage();
		}

		private void DashboardTab_Load(object sender, EventArgs e)
		{
			InitCatalogGrid();
		}

		private void InitCatalogGrid()
		{
			catalogGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
			rRecordedOn.DefaultCellStyle.Format = Settings.RenamePattern;
			rName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			rCamera.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			rSha1.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			catalogGrid.AutoGenerateColumns = false;
			//! VIRTUAL MODE
			//catalogGrid.VirtualMode = true;
			//catalogGrid.CellValueNeeded += catalogGrid_CellValueNeeded;

			catalogGrid.DataSource = Catalog;
		}

		void catalogGrid_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
		{
			var rec = Catalog[e.RowIndex];
			string col = catalogGrid.Columns[e.ColumnIndex].DataPropertyName;
			if (col == "Id")
				e.Value = rec.RecordingId;
			//else if (col == "InCatalog" ) //&& mv.InCatalog)
			//	e.Value = Properties.Resources.New_24;
			else if (col == "Name")
				e.Value = rec.Name;
			else if (col == "Type")
				e.Value = rec.TypeEnum;
			else if (col == "Camera")
				e.Value = rec.CameraEnum;
			else if (col == "RecordedOn" && rec.RecordedOn.HasValue)
				e.Value = rec.RecordedOn.Value;
			else if (col == "Size")
				e.Value = rec.Size;
			else if (col == "Sha1")
				e.Value = rec.Sha1;
		}

		private void catalogGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{
			var rec = Catalog[e.RowIndex];
			var row = catalogGrid.Rows[e.RowIndex];
			string col = catalogGrid.Columns[e.ColumnIndex].DataPropertyName;
			object val = row.Cells[e.ColumnIndex].Value;

			if (col == "TypeEnum")
			{
				e.Value = (int)val == (int)Enums.Type.Image ? Resources.Picture_16 : Resources.Movie_16;
			}
			else if (col.EndsWith("Enum"))
			{
				e.Value = Program.Enums.GetName((int?)val);
			}
		}


		private void cbShowSha1_CheckedChanged(object sender, EventArgs e)
		{
			catalogGrid.Columns["rSha1"].Visible = cbShowSha1.Checked;
		}
	}
}
